package com.zzx.boot.servlet.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzx.boot.constant.GlobalEnum;
import com.zzx.boot.entity.table.Follow;
import com.zzx.boot.entity.table.User;
import com.zzx.boot.entity.vo.UserVo;
import com.zzx.boot.exception.SystemctlException;
import com.zzx.boot.mapper.FollowMapper;
import com.zzx.boot.servlet.FollowService;
import com.zzx.boot.servlet.UserService;
import com.zzx.boot.utils.DateTImeUtils;
import com.zzx.boot.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author tan
 * @version 1.0
 * @date 2023/7/11 13:35
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements FollowService {

    @Resource
    private UserService userService;


    @Override
    public String addFollow(Long followId) {
        isUserExists(followId);
        // 是否已经关注过了
        if(reFollow(followId)){
            throw new SystemctlException(GlobalEnum.I_HAVE_ALREADY_FOLLOWED);
        }
        // 进行关注
        int i = baseMapper.insert(new Follow(null, followId, SecurityUtils.getUserId(), DateTImeUtils.getDate()));
        return i==1?"关注成功":"关注失败";
    }

    /**
     * 不能关注自己 并且该用户要存在
     * @param followId
     */
    private void isUserExists(Long followId) {
        // 检测是否是同一个用户
        if(SecurityUtils.getUserId().equals(followId)){
           throw new SystemctlException(GlobalEnum.UNABLE_TO_PAY_ATTENTION_TO_ONESELF);
        }
        // 检测该用户是否存在
        User user = userService.getById(followId);
        if(Objects.isNull(user)){
            throw new SystemctlException(GlobalEnum.USER_IS_NOT_REGISTERED);
        }
    }

    @Override
    public String removeFollow(Long followId) {
       // 不能关注自己 并且该用户要存在
        isUserExists(followId);
        if(!reFollow(followId)){
            throw new SystemctlException(GlobalEnum.USER_HAS_NOT_FOLLOWED);
        }
        LambdaQueryWrapper<Follow> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Follow::getFollowId,followId);
        wrapper.eq(Follow::getUserId,SecurityUtils.getUserId());
        int i = baseMapper.delete(wrapper);
        return i==1?"取消关注成功":"取消关注失败";
    }

    @Override
    public List<UserVo> mutualFollow(Long current) {
        //当前用户关注列表
        List<Follow> followList = followByUserId(null);
        // 检测其他用户有没有关注当前用户
        Page<Follow> page = new Page<>(current,10);
        List<Long> list = followList.stream().map(item -> item.getFollowId()).collect(Collectors.toList());
        isFenshiList(page, list);
        // 查出所有互相关注
        List<UserVo> vos = page.getRecords().stream().map(item -> userService.getUserById(item.getUserId())).collect(Collectors.toList());
        return vos;
    }



    @Override
    public List<UserVo> fansFollow(Long current) {
        Page<Follow> page = new Page<>(current,10);
        isFenshiList(page,null);
        List<UserVo> vos = page.getRecords().stream().map(item -> userService.getUserById(item.getUserId())).collect(Collectors.toList());
        return vos;
    }

    @Override
    public List<UserVo> followList(Long current) {
        Page<Follow> page = new Page<>(current, 10);
        List<Follow> followList = followByUserId(page);
        List<UserVo> vos = followList.stream().map(item -> userService.getUserById(item.getFollowId())).collect(Collectors.toList());
        return vos;
    }

    /**
     * 粉丝列表
     * @param page
     * @param list
     */
    private void isFenshiList(Page<Follow> page, List<Long> list) {
        LambdaQueryWrapper<Follow> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Follow::getFollowId,SecurityUtils.getUserId());
        if(list!=null){
            wrapper.in(Follow::getUserId, list);
        }
        baseMapper.selectPage(page,wrapper);
    }

    /**
     * 当前用户关注列表
     * @return
     */
    private List<Follow> followByUserId(Page page) {
        LambdaQueryWrapper<Follow> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Follow::getUserId, SecurityUtils.getUserId());
        if(Objects.isNull(page)){
            return baseMapper.selectList(wrapper);
        }
        return baseMapper.selectPage(page,wrapper).getRecords();
    }

    /**
     * 是否关注过了
     * @param followId
     */
    private boolean reFollow(Long followId) {
        LambdaQueryWrapper<Follow> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Follow::getFollowId, followId);
        wrapper.eq(Follow::getUserId,SecurityUtils.getUserId());
        Follow follow = baseMapper.selectOne(wrapper);

        return !Objects.isNull(follow);
    }
}
